<!DOCTYPE html>
<html>
<head>
    <title></title>
    <link rel="stylesheet" type="text/css" href="../lib/ext/resources/css/ext-all-neptune.css"/>
    <link rel="stylesheet" type="text/css" href="../lib/ckeditor/sample.css" />
    <script type="text/javascript" src="../lib/ext/ext-all.js"></script>
    <script type="text/javascript" src="../lib/gf/common_grid.js"></script>
</head>
<script type="text/javascript">
    Ext.Loader.setConfig({enabled: true});
    var dataForm;
    var data = {};
    Ext.onReady(function () {
        //保存数据
        function doAdd(){
            var params = dataForm.form.getValues();
            var _url = '/ad/add';
            if(params && params._id ){
                _url = '/ad/edit';
            }
            if(params.src_site && !params.src_land){
                alert('请填写着陆页面!');
                return;
            }
            Ext.Ajax.request({
                url: _url,
                params : params,
                method : 'POST',
                success: function(response, opts) {
                    alert('业务处理成功');
                    parent.refresh();
                    parent.closeWin();
                },
                failure: function(response, opts) {
                    alert('业务处理失败');
                }
            });

        }
        //关闭窗口
        function doClose(){
            parent.closeWin();
        }
        var tbar = [
            {pressed: true,text: '保  存',handler: doAdd,icon : '../img/book.png'},
            {pressed: true,text: "关  闭",handler: doClose,icon : '../img/close.png'}
        ];
        dataForm = new Ext.FormPanel({
            layout : 'fit',
            items: [
                {
                    xtype: 'form',
                    defaultType: 'textfield',
                    layout: {type: 'table',columns:3},
                    items: [
                        {
                            name : '_id',
                            hidden : true
                        },{
                            name : "_max_file_size",
                            value : "100000", //文件最大大小100k
                            hidden : true
                        },
                        {
                            fieldLabel: '广告名称',
                            name : 'name',
                            allowBlank: false
                        },
                        {
                            xtype: 'combobox',
                            fieldLabel: '所属栏目',
                            id : "ad_category",
                            name : 'ad_category',
                            labelAlign: 'right',
                            queryMode: 'local',
                            displayField: 'key_name',
                            value : 'none',
                            valueField: 'key_code',
                            allowBlank: false,
                            editable : false,
                            store: new Ext.data.JsonStore({
                                storeId: 'adStore',
                                autoLoad:true,
                                proxy: {
                                    type: 'ajax',
                                    url: '/dict/list?parent_code=ad_category',
                                    reader: {
                                        type: 'json',
                                        idProperty: 'key_code'
                                    }
                                },
                                fields: ['key_name', 'key_code']
                            })
                        },
                        {
                            fieldLabel: '广告编码',
                            name : 'code',
                            allowBlank: false
                        },                        
                        {
                            fieldLabel : '开始日期',
                            name : 'begin_date',
                            xtype : 'datefield',
                            editable  : false,
                            value : '20140101',
                            format: 'Ymd'
                        },
                        {
                            fieldLabel : '结束日期',
                            name : 'end_date',
                            xtype : 'datefield',
                            editable  : false,
                            value : '20170101',
                            format: 'Ymd'
                        },
                        {fieldLabel:'顺序',name:'sorder',xtype:'numberfield',value:0},
                        {
                            xtype: 'combobox',
                            fieldLabel: '显示方式',
                            id : "target",
                            name : 'target',
                            queryMode: 'local',
                            displayField: 'display',
                            valueField: 'value',
                            value : '_self',
                            editable : false,
                            colspan : 3,
                            store:  Ext.create('Ext.data.SimpleStore', {
                                fields: ['display', 'value'],
                                data : [['当前页','_self'],['空白页','_blank']]
                            })
                        },
                        {fieldLabel: '图片id',name: 'image_file_id',readOnly : true},
                        {xtype: "filefield",fieldLabel: '广告图片',name: 'attach_file'},
                        {
                            xtype: "button",
                            text: "上传图片",
                            width: 100,
                            handler: function () {
                                uploadFile(dataForm);
                            },
                            style: 'margin-left: 25px;'
                        },
                        {
                            fieldLabel : '广告内容',
                            name : 'content',
                            xtype : 'textareafield',
                            colspan:3,
                            width: 700
                        },{
                            fieldLabel : '广告站点',
                            name : 'src_site',
                            listeners :{
                                change : doGenHref
                            }
                        },{
                            fieldLabel : '站点页面',
                            name : 'src_page',
                            value : 'x',
                            colspan : 2,
                            listeners :{
                                change : doGenHref
                            }
                        },{
                            fieldLabel : '广告位置',
                            value : 'x',
                            name : 'src_pos',
                            listeners :{
                                change : doGenHref
                            }
                        }
                        ,{
                            xtype: 'combobox',
                            fieldLabel: '着陆页面',
                            id : "src_land",
                            name : 'src_land',
                            queryMode: 'local',
                            displayField: 'display',
                            valueField: 'value',
                            editable : false,
                            colspan : 2,
                            store:  Ext.create('Ext.data.SimpleStore', {
                                fields: ['display', 'value'],
                                data : [['home','home'],['minisite','minisite'],['wechat','wechat'],['mobile','mobile']]
                            }),
                            listeners :{
                                change : doGenHref
                            }
                        },
                        {
                            fieldLabel: '广告指向页面',
                            name : 'href',
                            id : 'href',
                            colspan : 3,
                            width : 700,
                            listeners :{
                                change : doGenHref
                            }
                        },
                        {
                            fieldLabel: '广告链接',
                            name : 'href2',
                            colspan : 2,
                            width : 700,
                            readOnly : true
                        }
                    ],
                    tbar : tbar
                }
            ]
        });
        var viewport = new Ext.Viewport({
            layout: 'fit',
            renderTo: "mypanel",
            items: [dataForm]
        });

        function doGenHref(){
            var params = dataForm.form.getValues();
            var href2 = params.href;
            if(params.src_site){
                var prefix = href2.indexOf('?') > 0 ? '&_gfsrc=' : '?_gfsrc=';
                href2 += prefix + params.src_site + '_' + params.src_page + '_'+ params.src_pos + '_' + params.src_land;
            }
            dataForm.form.findField('href2').setValue(href2);
        }



        //获取url中的参数，如果传入了参数_id那么进行修改；否则，添加。
        var _urlparams = Ext.urlDecode(document.URL.split("?")[document.URL.split("?").length - 1]);
        if(_urlparams && _urlparams._id){
            Ext.Ajax.request({
                url: '/ad/detail',
                params : {'_id' : _urlparams._id},
                method : 'get',
                success: function(response, opts) {
                    data =  Ext.decode(response.responseText);
                    console.log(data);
                    init_form();
                },
                failure: function(response, opts) {
                    alert('加载失败......');
                    init_form();
                }
            });
        }

        function init_form(){
            if(!data){
                return ;
            }
            for(var i in data){
                if(dataForm.form.findField(i)){
                    dataForm.form.findField(i).setValue(data[i]);
                }
            }
        }
    });
</script>
<body>
<div id="mypanel" style="height:1024px;width:100%"></div>
</body>
</html>